package com.ybxx.base.service.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.dao.BanDao;
import com.ybxx.base.dao.CommunityDao;
import com.ybxx.base.model.Ban;
import com.ybxx.base.model.Community;
import com.ybxx.base.service.CommunityService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.service.impl.BaseServiceImpl;

public class CommunityServiceImpl extends BaseServiceImpl<Community> implements
		CommunityService {
	private CommunityDao communityDao;
	private BanDao banDao;
	public CommunityServiceImpl(CommunityDao dao,BanDao banDao) {
		super(dao);
		this.communityDao=dao;
		this.banDao=banDao;
		// TODO Auto-generated constructor stub
	}
	/*检查管理费配置*/
	public int checkConfig(String unitid){
		String hql="from Community c " +
	            ",Ban b " +
	            ",Unit u " +
	            ",Room r "+
				"where c.cid=b.community.cid and b.bid=u.ban.bid and r.unit.uid=u.uid and (c.glprice is null or c.glprice='') ";
		if(unitid.indexOf("R")==0){
			hql+=" and r.rid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("U")==0){
			hql+=" and u.uid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("B")==0){
			hql+=" and b.bid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("C")==0){
			hql+=" and c.cid='"+unitid.substring(1)+"'";
		}
		return communityDao.findCountByHql(hql, null);
	}
	public Community show(String cid){
		String hql="from Community c left join fetch c.bans b left join fetch b.units u left join fetch u.rooms where c.cid='"+cid+"'";
		return (Community)this.communityDao.findUnique(hql, null);
	}
	public List<Community> queryNotFees(String loginid){
		List<Community> communitys=null;
		String hql="select distinct c from Fees f right join f.community c,DataRights m where c.cid=m.dvalue and m.dtype='C' and f.fid is null and m.user.loginid='"+loginid+"'";
		hql+=" order by c.state,c.cid";
		
		communitys=this.communityDao.findByHql(hql, null);
		return communitys;
	}
	public List<Community> queryNotHydropower(String loginid){
		List<Community> communitys=null;
		String hql="select distinct c from Hydropower h right join h.community c,DataRights m where c.cid=m.dvalue and m.dtype='C' and h.hcode is null and m.user.loginid='"+loginid+"'";
		hql+=" order by c.state,c.cid";
		
		communitys=this.communityDao.findByHql(hql, null);
		return communitys;
	}
	public List<Community> queryFeesPage(QueryFilter filter,String loginid){
		List<Community> communitys=null;
		String hql="select distinct c from Fees f inner join f.community c,DataRights m where c.cid=m.dvalue and m.dtype='C' and m.user.loginid='"+loginid+"'";
		hql+=" order by c.state,c.cid";
		
		communitys=this.communityDao.findByHql(hql, null,filter.getPagingBean());
		return communitys;
	}
	public List<Community> queryHydropowerPage(QueryFilter filter,String loginid){
		List<Community> communitys=null;
		String hql="select distinct c from Hydropower h inner join h.community c,DataRights m where c.cid=m.dvalue and m.dtype='C' and m.user.loginid='"+loginid+"'";
		hql+=" order by c.state,c.cid";
		
		communitys=this.communityDao.findByHql(hql, null,filter.getPagingBean());
		return communitys;
	}
	public Community queryFees(String cid) {
		String hql="select distinct c from Fees f right join f.community c where c.cid='"+cid+"'";
		return (Community)communityDao.findUnique(hql, null);
	}
	public List<Community> queryPage(QueryFilter filter,String name,String loginid){
		List<Community> communitys=null;
		String hql="select distinct c from Community as c,DataRights m where c.cid=m.dvalue and m.dtype='C' and m.user.loginid='"+loginid+"'";
		if(StringUtils.isNotEmpty(name)){
			hql+=" and c.cname like '%"+name+"%'";
		}
		hql+=" order by c.state,c.cid";
		
		communitys=this.communityDao.findByHql(hql, null, filter.getPagingBean());
		return communitys;
	}
	public void saveTH(Community community, List<Ban> bans){
		Community tmp=communityDao.get(community.getCid());
		tmp.setCnum(community.getCnum());
		tmp.setCrule(community.getCrule());
		tmp.setUnum(community.getUnum());
		tmp.setUrule(community.getUrule());
		tmp.setLnum(community.getLnum());
		tmp.setLrule(community.getLrule());
		tmp.setRnum(community.getRnum());
		tmp.setRrule(community.getRrule());
		String sql_1="delete from tbl_base_room where uid in (select uid from tbl_base_ban b,tbl_base_unit u where b.bid=u.bid and b.cid='"+community.getCid()+"')";
		String sql_2="delete from tbl_base_unit where bid in (select bid from tbl_base_ban b where b.cid='"+community.getCid()+"')";
		String sql_3="delete from tbl_base_ban where cid='"+community.getCid()+"'";
		communityDao.deleteSQL(sql_1,sql_2,sql_3);
		communityDao.save(tmp);
		for(Iterator<Ban> iter=bans.iterator();iter.hasNext();){
			Ban ban=iter.next();
			ban.setCommunity(tmp);
			banDao.save(ban);
		}
	}

	public List<Community> queryTree(String loginid) {
		List<Community> communitys=null;
		String hql="select distinct c from Community as c,DataRights m where c.cid=m.dvalue and m.dtype='C' and m.user.loginid='"+loginid+"' and c.state='0'";
		hql+=" order by c.state,c.cid";
		communitys=this.communityDao.findByHql(hql, null);
		return communitys;
	}
	public void saveBuildCharge(Community community) {
		String[] cids=community.getCid().split(",");
		String[] buildcharges=community.getBuildingcharge().split(",");
		String[] bccontents=community.getBccontent().split(",");
		for(int i=0;i<cids.length;i++){
			communityDao.excuteSql("update tbl_base_community set buildingcharge="+((buildcharges.length<=i || "".equals(buildcharges[i]))?0:buildcharges[i])+"" +
					",bccontent='"+((bccontents.length<=i)?"":bccontents[i])+"' where cid='"+cids[i]+"'");
		}
		
	}
	public void saveGLF(Community community) {
		String[] cids=community.getCid().split(",");
		String[] glrid=community.getGlrule().getCrid().split(",");
		String[] glprices=community.getGlprice().split(",");
		String[] glcontents=community.getGlcontent().split(",");
		for(int i=0;i<cids.length;i++){
			communityDao.excuteSql("update tbl_base_community set " +
					"glcrid='"+((glrid.length<=i)?"":glrid[i])+"'" +
					",glprice="+((glprices.length<=i || "".equals(glprices[i]))?0:glprices[i])+"" +
					",glcontent='"+((glcontents.length<=i)?"":glcontents[i])+"' where cid='"+cids[i]+"'");
		}
		
	}
	public List<Community> queryDataRightsAll(){
		List<Community> communitys=null;
		String hql="select distinct c from Community as c left join fetch c.glrule left join fetch c.roomrule,DataRights m where c.cid=m.dvalue and m.dtype='C'";
		communitys=this.communityDao.findByHql(hql, null);
		return communitys;
	}
	public void saveRoomFees(Community community) {
		String[] cids=community.getCid().split(",");
		String[] rrid=community.getRoomrule().getCrid().split(",");
		String[] rprices=community.getRprice().split(",");
		String[] rcontents=community.getRcontent().split(",");
		for(int i=0;i<cids.length;i++){
			communityDao.excuteSql("update tbl_base_community set " +
					"rcrid='"+((rrid.length<=i)?"":rrid[i])+"'" +
					",rprice="+((rprices.length<=i || "".equals(rprices[i]))?0:rprices[i])+"" +
					",rcontent='"+((rcontents.length<=i)?"":rcontents[i])+"' where cid='"+cids[i]+"'");
		}
	}
	public void saveHealthFees(Community community) {
		String[] cids=community.getCid().split(",");
		String[] hmonths=community.getHmonth().split(",");
		String[] hprices=community.getHprice().split(",");
		String[] hcontents=community.getHcontent().split(",");
		for(int i=0;i<cids.length;i++){
			communityDao.excuteSql("update tbl_base_community set " +
					"hmonth='"+((hmonths.length<=i)?"":hmonths[i])+"'" +
					",hprice="+((hprices.length<=i || "".equals(hprices[i]))?0:hprices[i])+"" +
					",hcontent='"+((hcontents.length<=i)?"":hcontents[i])+"' where cid='"+cids[i]+"'");
		}
		
	}
	

}
